package Main
{
	import flash.events.Event;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class WSController
	{
		private static var files:FileReferenceHandlers;
		public function WSController()
		{
		}
		
		public static function M2MTransformationService_faultHandler(event:FaultEvent, container:WSMDA_New):void {
			Alert.show(event.fault.message, "Error");
		}
		
		public static function addATL_faultHandler(event:FaultEvent, container:WSMDA_New):void {
			container.lilProgress.setVisible(false);
			initFileHandlers(container);
			if (event.fault.faultString.search(Util.ERR_CODE_ATL_SYNTAX) != -1) {
				Alert.show("Syntax error!");
			} else if (event.fault.faultString.search(Util.ERR_CODE_MISSING_MM) != -1 ) {
				UploadHandler.expandAction(container);
				container.M2MTransformationService.getSourceMetamodel(files.browserATLFile.name);
				container.M2MTransformationService.getTargetMetamodel(files.browserATLFile.name);
				Alert.show("ATL File uploaded. Now select the required metamodels.", "Info");
				UploadHandler.toggleUpButton(false, container);
				UploadHandler.toggleAtlButton(false, container);
				container.atlInput.setStyle("borderColor", 0x2813c8);
			} else {
				Alert.show("Unexpected error!");
			}
			
		}
		
		public static function addATL_resultHandler(container:WSMDA_New):void {
			container.lilProgress.setVisible(false);
			Alert.show("ATL Transformation " + container.atlInput.text.replace(".atl", "") + " uploaded\nWould you like to execute it?", "Success!", (Alert.YES | Alert.NO), null, UploadHandler.alertUploadingManager);
		}
		
		public static function addMM_faultHandler(event:FaultEvent, container:WSMDA_New):void {
			container.lilProgress.setVisible(false);
			Alert.show("Error while uploading the metamodel");
		}
		
		public static function addMM_resultHandler(event:ResultEvent, container:WSMDA_New):void {
			UploadHandler.undoAction(container);
		}
		
		public static function getTransformations(event:ResultEvent, container:WSMDA_New):void {
			try {
				if (event.result == null)
					return;
				if (event.result.toString().search(",") == -1) {
					var teste:String = event.result as String;
					container.availTransformations.dataProvider = teste;
				} else {
					var teste2:ArrayCollection = event.result as ArrayCollection;
					container.availTransformations.dataProvider = teste2;
				}
				
				if (container.atlInput != null) {
					TransformationHandler.selectTransformationByName(container.atlInput.text, container);
					
					UploadHandler.resetField(container, 0);
					UploadHandler.resetField(container, 1);
					UploadHandler.resetField(container, 2);
				}
				
			} catch (err:Error){
				Alert.show("Error while trying to retrieve transformation data");
				Alert.show(err.toString());
				
			}
		}
		
		public static function getTransformations_faultHandler(event:FaultEvent, container:WSMDA_New) {
			
		}
		
		public static function executeTransformation_resultHandler(event:ResultEvent, container:WSMDA_New):void {
			TransformationHandler.transformationFinished(container);
			TransformationHandler.showDownload(container, event.result as ByteArray);
			container.progress.setVisible(false);
			
		
		}
		
		public static function executeTransformation_faultHandler(event:FaultEvent, container:WSMDA_New):void {
			if (event.fault.faultString.search("Missing") != -1) {
				Alert.show("Some rules have dependencies. Select them.");
			} else {
				container.progress.setVisible(false);
				M2MTransformationService_faultHandler(event, container);
			}
		}
		
		public static function getSrcMM(event:ResultEvent, container:WSMDA_New):void {
			var metamodelName:String = event.result as String;
			if (container.currentState == "Uploading") {
				
				container.srcMMName.text = metamodelName + " type required";
				
			} else {
				
				container.sourceMMName.text = metamodelName;
				container.modelName.text = "Type: " + metamodelName;
			}
		}
		
		public static function getTrgtMM(event:ResultEvent, container:WSMDA_New):void {
			var metamodelName:String = event.result as String;
			if (container.currentState == "Uploading") {
				
				container.trgtMMName.text = metamodelName + " type required";
			} else {
				
				
				container.targetMMName.text = metamodelName;
				
			}
		}
		
		public static function getRules_handler(event:ResultEvent, container:WSMDA_New):void {
			try {
				if (event.result != null) {
					if (event.result.toString().search(",") == -1) {
						var string:String = event.result as String;
						var arr:ArrayCollection = new ArrayCollection;
						arr.addItem(string);
						var a2:ATLRules = new ATLRules(arr);
						container.atlRules.dataProvider = a2.getRules();
					} else {
						var array:ArrayCollection = event.result as ArrayCollection;
						var a:ATLRules = new ATLRules(array);
						container.atlRules.dataProvider = a.getRules();
					}
					
				}
			} catch (err:Error){
				Alert.show("Error while retrieving rules data");
				
			}
		}
		
		private static function initFileHandlers(wsmda:WSMDA_New): void {
			if (files == null) {
				files = FileReferenceHandlers.getInstance(wsmda);
			}
		}
	}
}